package com.cutefool.zero.code.admin.dbms.web;

import com.cutefool.core.account.Account;
import com.cutefool.core.account.LoginAccountService;
import com.cutefool.core.res.PageResponse;
import com.cutefool.core.res.Response;
import com.cutefool.dsl.mybatis.core.dsl.builder.DeleteDsl;
import com.cutefool.dsl.mybatis.core.dsl.builder.QueryDsl;
import com.cutefool.dsl.mybatis.core.dsl.condition.Conditioning;
import com.cutefool.dsl.mybatis.core.dsl.operation.CuteCurdOperation;
import com.cutefool.dsl.mybatis.core.page.Paging;
import com.cutefool.zero.code.admin.consts.Constants;
import com.cutefool.zero.code.admin.dbms.dto.DbmsDTO;
import com.cutefool.zero.code.admin.dbms.service.DbmsService;
import com.cutefool.zero.code.core.domain.Dbms;
import jakarta.annotation.Resource;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 相关接口
 *
 * @author : 271007729@qq.com
 */
@RestController
@RequestMapping(Constants.API_PREFIX)
public class DdmsController {

    @Resource
    private DbmsService dbmsService;

    @Resource
    private LoginAccountService<Account> loginAccountService;

    @Resource
    private CuteCurdOperation<Dbms> cuteCurdOperation;

    /**
     * 新增
     *
     * @param dto dto
     * @return Response<Long>
     **/
    @PostMapping("/add/dbms")
    public Response<Long> create(@RequestBody @Validated DbmsDTO dto) {
        return Response.ok(dbmsService.create(dto));
    }

    /**
     * 更新
     *
     * @param id  id
     * @param dto dto
     * @return Response<Long>
     **/
    @PostMapping("/update/dbms/{id}")
    public Response<Long> update(@PathVariable Long id, @RequestBody @Validated DbmsDTO dto) {
        dbmsService.update(dto);
        return Response.ok(id);
    }

    /**
     * 删除
     *
     * @param id id
     * @return Response<Boolean>
     **/
    @PostMapping("/delete/dbms/{id}")
    public Response<Boolean> delete(@PathVariable Long id) {
        DeleteDsl<Dbms> deleteDsl = DeleteDsl.newInstance(Dbms.instance(), (where) -> where.and(Dbms.instance().id(), SqlBuilder.isEqualTo(id)));
        this.cuteCurdOperation.delete(deleteDsl);
        return Response.ok();
    }


    /**
     * 查询（通过ID）
     *
     * @param id id
     * @return Response<Company>
     **/
    @GetMapping("/info/dbms/{id}")
    public Response<Dbms> byId(@PathVariable Long id) {
        QueryDsl<Dbms> queryDsl = QueryDsl.newInstance(Dbms.instance(), where -> where.and(Dbms.instance().id(), SqlBuilder.isEqualTo(id)));
        Optional<Dbms> optional = this.cuteCurdOperation.get(queryDsl);
        return Response.ok(optional.orElse(null));
    }

    /**
     * 查询（列表）
     *
     * @param query query
     * @return Response<List < Company>>
     **/
    @GetMapping("/list/dbms")
    public Response<List<Dbms>> list(Conditioning query) {
        query.setCreateBy(loginAccountService.getPresent().getId());
        QueryDsl<Dbms> queryDsl = QueryDsl.newInstance(Dbms.instance(), query);
        List<Dbms> entities = this.cuteCurdOperation.list(queryDsl);
        return Response.ok(entities);
    }

    /**
     * 查询（分页）
     *
     * @param query  query
     * @param paging paging
     * @return Response<PageResponse < Company>>
     **/
    @GetMapping("/page/dbms")
    public Response<PageResponse<Dbms>> page(Conditioning query, Paging paging) {
        query.setCreateBy(loginAccountService.getPresent().getId());
        QueryDsl<Dbms> queryDsl = QueryDsl.newInstance(Dbms.instance(), query, paging);
        PageResponse<Dbms> response = this.cuteCurdOperation.page(queryDsl);
        return Response.ok(response);
    }
}
